/****************************************************************************
 * 2025.4 周正正
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace ProjectSurvivor
{
	public partial class CoinUpgradePanel : UIElement, IController
	{
		void Refresh()
		{
			CoinUpgradeItemRoot.DestroyChildren();

			foreach (var coinUpgradeItem in this.GetSystem<CoinUpgradeSystem>().Items)
			{
				CoinUpgradeItemTemplate.InstantiateWithParent(CoinUpgradeItemRoot).Self(self =>
				{
					var itemCache = coinUpgradeItem;
					self.GetComponentInChildren<Text>().text = coinUpgradeItem.Description + $"{coinUpgradeItem.Price} 金币";
					self.onClick.AddListener(() =>
					{
						itemCache.Upgrade();
						AudioKit.PlaySound("ability_level_up");
					});
					var selfCache = self;
					Global.Coin.RegisterWithInitValue(coin =>
					{
						if (coin >= itemCache.Price)
						{
							selfCache.interactable = true;
						}
						else
						{
							selfCache.interactable = false;
						}
						//	CoinText.text = "金币:" + coin;
					}).UnRegisterWhenGameObjectDestroyed(self);
				}).Show();
			}

		}
		private void Awake()
		{
			CoinUpgradeItemTemplate.Hide();
			CoinUpgradeSystem.OnCoinUpgradeSystemChanged.Register(() =>
			{
				Refresh();
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			Refresh();
			Global.Coin.RegisterWithInitValue(coin =>
			{
				CoinText.text = "金币:" + coin;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			// BtnCoinPercentUpgrade.onClick.AddListener(() =>
			// {


			// });



			BtnClose.onClick.AddListener(() =>
			{
				this.Hide();
			});
		}

		protected override void OnBeforeDestroy()
		{
		}

		public IArchitecture GetArchitecture()
		{
			return Global.Interface;
		}
	}
}